//
//  AkuMainTabBar.swift
//  AkuSwiftSina
//
//  Created by 阿酷 on 16/7/19.
//  Copyright © 2016年 AkuApp. All rights reserved.
//

import UIKit

class AkuMainTabBar: UITabBar {

    /// 点击centerButton 的回调
    var didCenterBtn: (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        /// 添加 中间的 按钮
        addSubview(centerButton)
        // 让 centerButton 显示在最中央
        centerButton.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
    }
    
    /// 点击 centerButton 的 监听方法  // 私有方法添加 @objc
    @objc private func didCenterButton() {
        
        print("点击了")
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        

        //MARK: - 重新布局 子控件
        var index: CGFloat = 0
        let butWidth: CGFloat = self.frame.width / 5
        
        for obj in subviews {
            
            if obj.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                obj.frame.origin.x = index * butWidth
                obj.frame.size.width = butWidth
                
                index++
                
                if index == 2 {
                    
                    index++
                }
            }
        }

        
        
        
    }
    
    /// 懒加载 中间的 centerbutton
    lazy var centerButton: UIButton = {
    
        let button: UIButton = UIButton()

        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        // 高度自适应
        button.sizeToFit()
        
        // 添加监听
        button.addTarget(self, action: "didCenterButton", forControlEvents: .TouchUpInside)
        
        return button
    }()
    
}
